package com.expense.app.admin.services.impl;

import cn.hutool.core.convert.Convert;
import com.expense.app.admin.common.BaseService;
import com.expense.app.admin.model.dto.adminRole.AdminRoleDetailDTO;
import com.expense.app.admin.model.dto.adminRole.AdminRoleListDTO;
import com.expense.app.admin.model.params.adminRole.AdminRoleDetailSaveParam;
import com.expense.app.admin.model.params.adminRole.AdminRoleListParam;
import com.expense.app.admin.model.params.adminRole.AdminRoleStatusParam;
import com.expense.app.admin.services.AdminRoleServ;
import com.expense.common.constant.enums.AvailableStatus;
import com.expense.common.constant.enums.DeletedStatus;
import com.expense.common.dao.auth.AdminMenuRoleDao;
import com.expense.common.dao.auth.AdminRoleDao;
import com.expense.common.exception.PlatformException;
import com.expense.common.model.auth.AdminMenuRole;
import com.expense.common.model.auth.AdminRole;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class AdminRoleServImpl extends BaseService implements AdminRoleServ {

    @Autowired
    AdminRoleDao adminRoleDao;

    @Autowired
    AdminMenuRoleDao adminMenuRoleDao;

    @Override
    public AdminRoleListDTO findAllList() {
        List<AdminRole> list = adminRoleDao.findAvailableRole();
        Long total = list!=null?Long.valueOf(list.size()):0l;
        return new AdminRoleListDTO(list,total);
    }
    @Override
    public AdminRoleListDTO findList(AdminRoleListParam param) {
        Map<String,Object> condition = new HashMap<>();
        condition.put("key",param.getKey());
        condition.put("availableStatus",param.getAvailableStatus());
        PageQuery pageQuery = initPageQuery(param.getPage(),param.getSort());
        pageQuery.setParas(condition);
        adminRoleDao.findListPage(pageQuery);
        return new AdminRoleListDTO(pageQuery.getList(),pageQuery.getTotalRow());
    }

    @Override
    public AdminRoleListDTO getAllRoles() {
        return new AdminRoleListDTO(adminRoleDao.getAllRole());
    }

    @Override
    public AdminRoleDetailDTO findDetail(Long id) {
        if(id <= 0) throw new PlatformException("没有查询到角色信息");
        AdminRole adminRole = adminRoleDao.findByIdAndDeleted(id);
        List<Long> menuIds = adminMenuRoleDao.findMenuIdByRoleIdAndIsLeafAndUndeleted(adminRole.getId());
        adminRole.setMenuIds(menuIds);
        return new AdminRoleDetailDTO(adminRole);
    }

    @Override
    public Long saveOne(AdminRoleDetailSaveParam param) {
        AdminRole adminRole = param.getEntity();
        List<Long> menuIds = adminRole.getMenuIds();
        setBaseInfo(adminRole);
        Boolean isUpdate = Convert.toLong(adminRole.getId(),0l) > 0?true:false;
        if(!isUpdate) adminRole.setAvailableStatus(AvailableStatus.AVAILABLE.getValue());
        adminRoleDao.saveOne(adminRole);

        if(isUpdate) adminMenuRoleDao.deleteByRoleId(adminRole.getId(),getCurrentUser().getId());
        List<AdminMenuRole> adminMenuRoleList = new ArrayList<>();
        if(menuIds!=null && menuIds.size()>0){
            menuIds.stream().forEach(menuId -> {
                AdminMenuRole menuRole = new AdminMenuRole();
                menuRole.setAdminMenuId(menuId);
                menuRole.setAdminRoleId(adminRole.getId());
                setBaseInfo(menuRole);
                adminMenuRoleList.add(menuRole);
            });
            adminMenuRoleDao.insertBatch(adminMenuRoleList);
        }
        return adminRole.getId();
    }

    @Override
    public Long updateStatus(AdminRoleStatusParam param) {
        AdminRole adminRole = adminRoleDao.findByIdAndDeleted(param.getId());
        if(adminRole == null) throw new PlatformException("没有查询到角色信息");
        adminRole.setAvailableStatus(param.getStatus());
        setBaseInfo(adminRole);
        adminRoleDao.updateById(adminRole);
        return adminRole.getId();
    }

    @Override
    public Long deleteOne(Long id) {
        if(id <= 0) throw new PlatformException("请选择删除的角色");
        AdminRole adminRole = adminRoleDao.findByIdAndDeleted(id);
        if(adminRole == null) throw new PlatformException("角色已不存在");
        setBaseInfo(adminRole);
        adminRole.setDeleted(DeletedStatus.DELETED);
        adminRoleDao.updateTemplateById(adminRole);
        adminMenuRoleDao.deleteByRoleId(id,getCurrentUser().getId());
        return id;
    }
}
